package com.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.demo.http.HttpClientService;
import com.demo.io.ReadImage2Byte;
import dto.TestBo;
import dto.TestDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.entity.StringEntity;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author jasper
 */
@Slf4j
@RestController
@RequestMapping("/call")
public class ImageCallController {
    @Autowired
    private HttpClientService httpClientService;
    @Value("${local.image.path}")
    private String rootPath;

    @GetMapping("/image")
    public JSONObject call(String imageName, String imageUrl, String rUrl, String side, String thirdParty) {

        byte[] bytes = ReadImage2Byte.readImage2ByteArray(rootPath + imageName);
        String department = "iot";
        String os = "bigdata";

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("image", bytes);
        jsonObject.put("side", side);
        jsonObject.put("department", department);
        jsonObject.put("os", os);
        jsonObject.put("thirdParty", thirdParty);
        jsonObject.put("url", imageUrl);

        StringEntity stringEntity = httpClientService.getStringEntity(jsonObject);


        return httpClientService.postRequest(stringEntity, rUrl);
    }

    @GetMapping("/imageByUrl")
    public JSONObject call(String url, String side, String thirdParty) {

        byte[] bytes = ReadImage2Byte.readUrl2ByteArray("http://shprd.oss-cn-shanghai.aliyuncs.com/qrcode/Lark20200513-112128.jpeg");
        String department = "iot";
        String os = "bigdata";

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("image", bytes);
        jsonObject.put("side", side);
        jsonObject.put("department", department);
        jsonObject.put("os", os);
        jsonObject.put("thirdParty", thirdParty);
        jsonObject.put("url", "http://shprd.oss-cn-shanghai.aliyuncs.com/qrcode/Lark20200513-112128.jpeg");

        StringEntity stringEntity = httpClientService.getStringEntity(jsonObject);


        return httpClientService.postRequest(stringEntity, url);
    }

    @PostMapping("/testA")
    public void test(@RequestBody @Validated TestDto testDto) {
        long id = testDto.getId();
        String name = testDto.getName();
        Map<String, String> sub = testDto.getSub();
        TestBo testBo = new TestBo();
        BeanUtils.copyProperties(testDto, testBo);
        System.out.println();
    }

    @PostMapping("/testB")
    public void test2(@RequestBody JSONObject js) {
        String s = js.toJSONString();
    }
}
